package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.ArtInfoExample;
import cn.shlbysj.demo.entity.NewsDiscusInfo;
import cn.shlbysj.demo.entity.NewsDiscusInfoExample;
import cn.shlbysj.demo.mapper.ArtInfoMapper;
import cn.shlbysj.demo.mapper.NewsDiscusInfoMapper;
import cn.shlbysj.demo.service.INewsDiscusInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.sun.tools.doclets.formats.html.markup.HtmlStyle.title;

@Service("newsDiscusInfoService")
@Transactional
public class INewsDiscusInfoServiceImpl implements INewsDiscusInfoService{

    @Autowired
    private NewsDiscusInfoMapper newsDiscusInfoDao;

    @Override
    public PageResult findPage(String newsId, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        NewsDiscusInfoExample example=new NewsDiscusInfoExample();
        example.setOrderByClause("`create_time` desc");
        NewsDiscusInfoExample.Criteria criteria = example.createCriteria();
        criteria.andNewsIdEqualTo(newsId);
        Page<NewsDiscusInfo> pageList = (Page<NewsDiscusInfo>) newsDiscusInfoDao.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public Result insert(NewsDiscusInfo newsDiscusInfo) {
        newsDiscusInfo.setdId(UuidUtil.getUUID());
        newsDiscusInfo.setCreateTime(new Date());
        newsDiscusInfo.setIsReplay(ConstansUtil.DIS_NO_REPLAY);
        int ret=newsDiscusInfoDao.insert(newsDiscusInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result update(NewsDiscusInfo newsDiscusInfo) {
        int ret=newsDiscusInfoDao.updateByPrimaryKeySelective(newsDiscusInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result delete(String dId) {
        int ret=newsDiscusInfoDao.deleteByPrimaryKey(dId);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
}
